package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwekT;
import cn.e4j.modular.crm.mapper.GwekTMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 月汇率 服务实现类
 *
 * @author yuan_rz
 * @since 2019-01-10
 */
@Service
public class Gwei014ServiceImpl extends ServiceImpl<GwekTMapper, GwekT>{

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getListData(Page<Object> page, Dto dto) {
//        List<Map<String, Object>> listData = this.baseMapper.getListData(page, dto);
        List<Map<String, Object>> listData = InvokeUtil.invokeBeanMethod("gwekTMapper,getListData",page,dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Boolean existedTheParams(GwekT gwekT) {
        QueryWrapper<GwekT> gwekTQueryWrapper = new QueryWrapper<>();
        gwekTQueryWrapper.eq("GWEKENT",gwekT.getGWEKENT());
        gwekTQueryWrapper.eq("GWEK001",gwekT.getGWEK001());
        gwekTQueryWrapper.eq("GWEK002",gwekT.getGWEK002());
        return this.baseMapper.selectList(gwekTQueryWrapper).size() > 0;
    }

    @Transactional
    public void addData(GwekT gwekT){
        gwekT.insert();
    }

    @Transactional
    public void editData(GwekT gwekT,GwekT oldGwekT){
        UpdateWrapper<GwekT> gwekTUpdateWrapper = new UpdateWrapper<>();
        gwekTUpdateWrapper.eq("GWEKENT",oldGwekT.getGWEKENT());
        gwekTUpdateWrapper.eq("GWEK001",oldGwekT.getGWEK001());
        gwekTUpdateWrapper.eq("GWEK002",oldGwekT.getGWEK002());
        gwekT.update(gwekTUpdateWrapper);
    }

    @Transactional
    public void deleteData(GwekT gwekT){
        delete(gwekT);
    }

    @Transactional
    public void deleteDataBatch(List<GwekT> gwekTList){
        for(int i=0; i < gwekTList.size(); i++){
            delete(gwekTList.get(i));
        }
    }

    private void delete(GwekT gwekT){
        //修改1
        UpdateWrapper<GwekT> gwekTUpdateWrapper = new UpdateWrapper<>();
        gwekTUpdateWrapper.eq("GWEKENT",gwekT.getGWEKENT());
        gwekTUpdateWrapper.eq("GWEK001",gwekT.getGWEK001());
        gwekTUpdateWrapper.eq("GWEK002",gwekT.getGWEK002());
        gwekT.delete(gwekTUpdateWrapper);
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwacList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> listData = this.baseMapper.listGwacPage(page, dto);
        return listData;
    }
}
